﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BenhVien
{
    public partial class frmLogin : Form
    {
        private SqlConnection con;
        private SqlDataAdapter da=new SqlDataAdapter();
        private DataTable dt=new DataTable("DangNhap1");

        public frmLogin()
        {
            InitializeComponent();
        }

        private void connect() 
        {

            con = new SqlConnection("Data Source=192.168.1.200,1433;server=IWANTWIN-PC; user id=sa; password=123456; database=QuanLyHocSinh; Connection Timeout=15");

            con = new SqlConnection("Server=192.168.1.200;user id=sa;password=123456;database=QLBenhVien");

            try
            {
                con.Open();
                
            }
            catch
            {
                MessageBox.Show("ket noi that bai", "Xem lai", MessageBoxButtons.OK);
            }
        }
        
        private void disconnect()
        {
            con.Close();
            con.Dispose();
            con = null;
        }
       

        
        private void frmLogin_Load(object sender, EventArgs e)
        {
            connect();
            this.AcceptButton = btDangNhap;
            
        }


        private void btThoat_Click(object sender, EventArgs e)
        {
            disconnect();
            Application.Exit();
        }

        private void btDangNhap_Click_1(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * From DangNhap
                                            Where (HoTen = @HoTen) 
                                            And (MatKhau = @MatKhau)";
            command.Parameters.Add("@HoTen", SqlDbType.NVarChar, 50).Value = txtName.Text;
            command.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 50).Value = txtMatKhau.Text;
            da.SelectCommand = command;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                FormChinh formchinh = new FormChinh();
                formchinh.Show();
                Hide();
            }
            else
            {
                if (MessageBox.Show("dang nhap that bai, ban co muon dang nhap lai khong?", "Dang Nhap", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    txtName.Focus();
                }
                else
                {
                    disconnect();
                    Application.Exit();
                }
            }
        }
    }
}
